package com.tigerlee.candle.spreadsheet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.tigerlee.candle.util.Constants;

import android.util.Log;

public class ChatServerFeedParser {
	private XmlPullParserFactory mXmlPullParserFactory;
	
	
	private static final String TAG_IP = "gsx:serverip";
	private static final String TAG_PORT = "gsx:serverport";
	private static final String TAG_LANGUAGE = "gsx:language";
	

	public ChatServerFeedParser() {
		// Initialize every array in FeedParedData
		ChatServerParsedData.index = 0;
		ChatServerParsedData.mIPServer = new Vector<String>();
		ChatServerParsedData.mPortServer = new Vector<Integer>();
		ChatServerParsedData.mLanguageServer = new Vector<String>();
	}
	public boolean getParedData(InputStream inputStream) {
		try {
			mXmlPullParserFactory = XmlPullParserFactory.newInstance();
			XmlPullParser mXmlPullParser = mXmlPullParserFactory.newPullParser();
			mXmlPullParser.setInput(inputStream, null);
			
			int mEventType = mXmlPullParser.getEventType();
			

			String tag = null;
			
			boolean isIPTag = false;
			boolean isPortTag = false;
			boolean isLanguageTag = false;
			
			while (mEventType != XmlPullParser.END_DOCUMENT) {
				switch (mEventType) {
				case XmlPullParser.START_TAG:
					tag = mXmlPullParser.getName();
					if(tag.compareTo(TAG_IP) == 0){
						ChatServerParsedData.index++;
						isIPTag = true;
						mEventType = mXmlPullParser.next();
					}
					else if (tag.compareTo(TAG_PORT) == 0) {
						isPortTag = true;
						mEventType = mXmlPullParser.next();
					}
					else if (tag.compareTo(TAG_LANGUAGE) == 0) {
						isLanguageTag = true;
						mEventType = mXmlPullParser.next();
					} else {
						break;
					}
				case XmlPullParser.TEXT:
					try{
						if (isIPTag) {
							ChatServerParsedData.mIPServer.add(mXmlPullParser.getText());
						} else if(isPortTag){
							ChatServerParsedData.mPortServer.add(Integer.valueOf(mXmlPullParser.getText()));
						} else if (isLanguageTag) {
							ChatServerParsedData.mLanguageServer.add(mXmlPullParser.getText());
						} else {
							break;
						}
					}catch (NullPointerException e){
						e.printStackTrace();
						if(ChatServerParsedData.mIPServer.size() >= ChatServerParsedData.index){
							ChatServerParsedData.mIPServer.remove(ChatServerParsedData.index);
						}
						if(ChatServerParsedData.mPortServer.size() >= ChatServerParsedData.index){
							ChatServerParsedData.mPortServer.remove(ChatServerParsedData.index);
						}
						if(ChatServerParsedData.mLanguageServer.size() >= ChatServerParsedData.index){
							ChatServerParsedData.mLanguageServer.remove(ChatServerParsedData.index);
						}
					}
					
				case XmlPullParser.END_TAG:
					isIPTag = false;
					isPortTag = false;
					isLanguageTag = false;
					break;
				}
				mEventType = mXmlPullParser.next();
			}
		} catch (XmlPullParserException e) {
			if(Constants.LOG_VISIBLE) Log.d("XmlPullParserException", e.getMessage());
			return false;
			
		} catch (IOException e) {
			if(Constants.LOG_VISIBLE) Log.d("IOException", e.getMessage());
			return false;
		}
		if(ChatServerParsedData.index != 0){
			return true;
		}else{
			return false;
		}
	}

}
